package com.mach.platform.sprider.opc;

import lombok.extern.slf4j.Slf4j;
import org.openscada.opc.lib.common.ConnectionInformation;
import org.openscada.opc.lib.da.Server;
import org.openscada.opc.lib.da.SyncAccess;
import org.openscada.opc.lib.list.ServerList;

import java.util.HashMap;
import java.util.concurrent.Executors;

@Slf4j
public class OPCDAServer {

    private  static  final HashMap<String, OPCDAServer> devs = new HashMap<String, OPCDAServer>();
    private String ip = null;
    private String userName = null;
    private String passwd = null;
    private String progId = null;
    private String domain = null;

    private String clsId = null;
    private SyncAccess access = null;
    private Server server = null;

    public static OPCDAServer getOrNew(
            OPCDADev dev){
//        OPCDADev  devNew = new OPCDADev(devInfoMap, sqlHelper, dataLogService);

        OPCDAServer server = devs.get(dev.getServerKey());
        if(server == null){
            server = new OPCDAServer();;
            devs.put(dev.getServerKey(), server);
        }

        server.ip = dev.getIp();
        server.progId = dev.getProgid();
        server.domain = dev.getDomain();
        server.userName = dev.getUserName();
        server.passwd = dev.getPasswd();

        server.init();

        return server;
    }

    private void init() {

        this.flushClsId();

        ConnectionInformation ci = new ConnectionInformation();
        ci.setHost(this.ip);
        ci.setDomain(this.domain);
        ci.setUser(this.userName);
        ci.setPassword(this.passwd);
        ci.setProgId(null);
        ci.setClsid(this.clsId);

        if(clsId == null){
            ci.setProgId(this.progId);
        }

        this. server = new Server(
                ci,
                Executors.newSingleThreadScheduledExecutor());

        try {


            //this.access = new SyncAccess(server, PERIOD);


        } catch (Exception e) {
//            e.printStackTrace();
            log.info("OPCDAServer Connect... excep:" + e.getLocalizedMessage());
        }

    }

    private void flushClsId() {
        try {
            ServerList serverList = new ServerList(this.ip,
                    this.userName, this.passwd,
                    this.domain);

            this.clsId = serverList
                    .getClsIdFromProgId(this.progId);


        } catch (Exception e) {
//            e.printStackTrace();
            log.info("OPCDAServer flushClsId... excep:" + e.getLocalizedMessage());
        }
    }

    /*
    未写完
     */
    public void regDev(OPCDADev dev){
        if(this.access == null){
            log.info("OPCDAServer regDev accessObj is null.");
            return;
        }

    }

    public void restart(){

    }

    public static String opcServerKey(String ip, String progid){
        return ip+":" + progid;
    }

    public Server getRealServer() {
        return this.server;
    }

    public void dispose() {
        this.server.dispose();
    }
}
